package com.yeshine.opendata.controller.front.front;

import com.yeshine.opendata.controller.front.AbstractFrontControllerBase;
import com.yeshine.opendata.entity.App;
import com.yeshine.opendata.entity.AppTheme;
import com.yeshine.opendata.entity.SysDictionary;
import com.yeshine.opendata.service.IAppService;
import com.yeshine.opendata.service.IAppThemeService;
import com.yeshine.opendata.service.IDictionaryService;
import com.yeshine.opendata.utils.PageResult;
import com.yeshine.opendata.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by taohj on 2014/8/1.
 */
@Controller
@RequestMapping("/front/front/app")
public class AppController extends AbstractFrontControllerBase {

    @Autowired
    private IAppService service;

    @Autowired
    private IAppThemeService themeService;

    @Autowired
    private IDictionaryService dictionaryService;

    @RequestMapping("list")
    public String list(
            @RequestParam(value = "theme", required = false) Integer[] theme,
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "order", required = false) String order,
            @RequestParam(value = "platform", required = false) Integer[] platform
    ) {
        App app = new App();
        app.setVisibility(new SysDictionary(141));
        if (StringUtils.hasLength(name)) {
            app.setName(name);
        }
        if (theme != null && theme.length > 0) {
            app.setThemes(theme);
        }
        if (platform != null && platform.length > 0) {
            app.setPlatforms(platform);
        }

        PageResult<App> pageResult = new PageResult<App>(1, 999999999);
        if (StringUtils.hasLength(order)) {
            pageResult.setSortBy(order);
            pageResult.setSortColumn("app.name");
        }
        pageResult.setT(app);
        Result<App> result = service.getByPage(pageResult);
        List<AppTheme> themes = themeService.getList(null).getList();
        setRequestAttr("themes", themes);


        SysDictionary appPlatform = new SysDictionary();
        appPlatform.setType("APP_PLATFORM");
        List<SysDictionary> platforms = dictionaryService.getList(appPlatform).getList();
        setRequestAttr("platforms", platforms);

        setRequestAttr("result", result);
        setRequestAttr("theme", theme);
        setRequestAttr("platform", platform);
        return "/front/front/app/list";
    }


    @ResponseBody
    @RequestMapping("view")
    public String detail(
            @RequestParam("id") Integer id
    ) {
        Result<App> result = service.getById(id);
        return result.getTarget().toString();
    }
}
